﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace EM.FrameWork
{
    /// <summary>枚举帮助类</summary>
    public static class EnumHelper
    {
        /// <summary> 获取枚举类型的描述</summary>
        public static string EnumDescription(System.Enum value)
        {
            string result = string.Empty;
            FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
            if (fieldInfo != null)
            {
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attributes.Length > 0)
                    result = attributes[0].Description;
            }
            return result;
        }

        /// <summary> 获取一个枚举的全部成员描述</summary>
        public static Dictionary<T, string> EnumDescriptions<T>()
        {
            Dictionary<T, string> result = new Dictionary<T, string>();
            List<T> items = EnumItems<T>();
            foreach (T item in items)
            {
                FieldInfo fieldInfo = item.GetType().GetField(item.ToString());
                if (fieldInfo != null)
                {
                    DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (attributes.Length > 0)
                        result.Add(item, attributes[0].Description);
                }
            }
            return result;
        }

        /// <summary> 获取一个枚举的全部成员描述(已经忽略的成员将不再字典中) </summary>
        public static Dictionary<T, string> EnumDescriptionsII<T>()
        {
            Dictionary<T, string> result = new Dictionary<T, string>();
            List<T> items = EnumItems<T>();
            foreach (T item in items)
            {
                FieldInfo fieldInfo = item.GetType().GetField(item.ToString());
                if (fieldInfo != null)
                {
                    Ignore[] attributeIgnore = (Ignore[])fieldInfo.GetCustomAttributes(typeof(Ignore), false);
                    if (attributeIgnore != null && attributeIgnore.Length > 0)
                        continue;

                    DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (attributes.Length > 0)
                        result.Add(item, attributes[0].Description);
                }
            }
            return result;
        }

        /// <summary>根据枚举描述获取枚举值 </summary>
        public static T GetEnumValueByDescription<T>(string description)
        {
            Dictionary<T, string> descriptions = EnumDescriptions<T>();
            foreach (KeyValuePair<T, string> kv in descriptions)
            {
                if (string.Equals(kv.Value, description, StringComparison.OrdinalIgnoreCase))
                    return kv.Key;
            }
            return default(T);
        }

        /// <summary> 获取一个枚举定义的全部成员。</summary>
        public static List<T> EnumItems<T>()
        {
            List<T> result = new List<T>();
            Type enumType = typeof(T);
            foreach (string typeName in Enum.GetNames(enumType))
                result.Add((T)Enum.Parse(enumType, typeName));
            return result;
        }

        /// <summary>
        /// 获取枚举中全部描述和整型值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static NameValueCollection GetEnumDescriptionAndValues<T>()
        {
            NameValueCollection coll = new NameValueCollection();
            Array values = Enum.GetValues(typeof(T));
            foreach (var item in values)
            {
                Enum en = (Enum)Enum.Parse(typeof(T), item.ToString(), true);
                string description = EnumDescription(en);
                coll.Add(description, ((int)Enum.Parse(typeof(T), item.ToString(), true)).ToString());
            }
            return coll;
        }

        /// <summary>
        /// 根据枚举值获取描述信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetEnumDescription<T>(int value)
        {
            string result = "";
            Enum en = (Enum)Enum.Parse(typeof(T), value.ToString(), true);
            result = EnumDescription(en);
            return result;
        }
    }
}
